# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::server::Session;
use strict;

use Settings;
use HTTP::Request::Common;
use HTTP::Cookies;
use LWP::UserAgent;
use URI::Escape qw(uri_escape_utf8);

binmode STDERR, ":utf8";

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self  = {};
	$self->{UserName} = shift;
	$self->{Password} = shift;
	$self->{HostName} = $Settings::GameHost;
	$self->{UserAgent} = LWP::UserAgent->new();
	$self->{UserAgent}->cookie_jar({});
	$self->{Open} = 0;
	$self->{Downloaded} = 0;
	bless ($self, $class);
	return $self;
}

sub IsOpen
{
	my $self = shift;
	return $self->{Open};
}

sub request
{
	my $self = shift;
	my $req = shift;
	my $res = $self->{UserAgent}->request($req);
	$self->{Downloaded} += length($res->as_string);
	print STDERR ($res->decoded_content ? $res->decoded_content : $res->as_string)."\n" if ($Settings::Debug && !$res->is_success);
	return $res;
}

sub Open
{
	my $self = shift;
	my $req = POST "http://".$self->{HostName}."/overview/",
		Content => "action=login&login=".uri_escape_utf8($self->{UserName})."&pwd=".uri_escape_utf8($self->{Password});
	my $res = $self->request($req);
	$self->{Open} = $res->is_success();
	$self->{ID} = ($res->decoded_content =~ m|p=(\d+)_(\d)|)[0] unless defined $self->{ID};
	return $self->{Open};
}

sub Close
{
	my $self = shift;
	my $req = HEAD "http://".$self->{HostName}."/logoff/";
	my $res = $self->request($req);
	$self->{Open} = 0;
	return $res->is_success();
}

sub Get
{
	my $self = shift;
	my $link = shift;
	$self->Open unless $self->IsOpen();
	return undef unless $self->IsOpen();
	my $req = GET "http://".$self->{HostName}.$link;
	my $res = $self->request($req);
	return $res->is_success() ? $res->decoded_content : undef;
}

sub GetXML
{
	my $self = shift;
	my $datatype = shift;
	my $req = POST "http://".$self->{HostName}."/frames/empire_info/on/$datatype/asxml/", 
		Content => "action=login&login=".uri_escape_utf8($self->{UserName})."&pwd=".uri_escape_utf8($self->{Password});
	my $res = $self->request($req);
	return $res->is_success() ? $res->decoded_content : undef;
}

sub PostXML
{
	my $self = shift;
	my $data = shift;
	my $req = POST "http://".$self->{HostName}."/frames/perform_x_actions/on/1/asxml/", 
		Content => "action=login&login=".uri_escape_utf8($self->{UserName})."&pwd=".uri_escape_utf8($self->{Password})."&xactions=".uri_escape_utf8($data);
	my $res = $self->request($req);
	return $res->is_success() ? $res->decoded_content : undef;
}

sub GetDownloaded
{
	my $self = shift;
	return $self->{Downloaded};
}

1;
